using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class ChatMultyPicArticleItem : BaseChatElement
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "ChatMultyPicArticleItem";
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(AppMsgInfo), typeof(ChatMultyPicArticleItem), new PropertyMetadata(null, new PropertyChangedCallback(ChatMultyPicArticleItem.OnItemObjectPropertyChanged)));
		private MenuItem ContentMenuItem;
		private Panel panelItems;
		private TextBlock txtTitle;
		private TextBlock txtDigest;
		private TextBlock txtTitleMulty;
		private TextBlock txtTime;
		private TextBlock TimeTxt2;
		private MMHttpImg imgCover;
		private Button btnTopItem;
		private Button btnRoot;
		private new BaseChatContent ChatContentCtrl;
		private ContextMenu contextMenu;
		private MenuItem DeleteMsgMenu;
		private bool mBIsTemplateApplyted;
		private AppMsgInfo mMsgInfo;
		private static readonly Thickness sMarginItems = new Thickness(0.0, 1.0, 0.0, 0.0);
		public static DependencyProperty FlagImgSourceProperty = DependencyProperty.Register("FlagImgSource", typeof(ImageSource), typeof(ChatMultyPicArticleItem), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/ffmsg/qq.png") as ImageSource, new PropertyChangedCallback(ChatMultyPicArticleItem.OnFlagImgSourceChanged)));
		public static DependencyProperty HeadImgSourceProperty = DependencyProperty.Register("HeadImgSource", typeof(ImageSource), typeof(ChatMultyPicArticleItem), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/CommonUse/DefaultHead.png") as ImageSource, new PropertyChangedCallback(ChatMultyPicArticleItem.OnHeadImgSourceChanged)));
		public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ChatMultyPicArticleItem), new PropertyMetadata(strings.Con_FMsg_TxtName, new PropertyChangedCallback(ChatMultyPicArticleItem.OnTitleChanged)));
		public static DependencyProperty NickNameProperty = DependencyProperty.Register("NickName", typeof(string), typeof(ChatMultyPicArticleItem), new PropertyMetadata("用户昵称", new PropertyChangedCallback(ChatMultyPicArticleItem.OnNickNameChanged)));
		public static DependencyProperty UserInfoProperty = DependencyProperty.Register("UserInfo", typeof(string), typeof(ChatMultyPicArticleItem), new PropertyMetadata("微信号:XXX", new PropertyChangedCallback(ChatMultyPicArticleItem.OnUserInfoChanged)));
		private bool _contentLoaded;
		public event ChatMultyPicArticleItem.ListItemClick ItemClick;
		public AppMsgInfo ItemObject
		{
			get
			{
				return (AppMsgInfo)base.GetValue(ChatMultyPicArticleItem.ItemObjectProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.ItemObjectProperty, value);
			}
		}
		public ImageSource FlagImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatMultyPicArticleItem.FlagImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.FlagImgSourceProperty, value);
			}
		}
		public ImageSource HeadImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatMultyPicArticleItem.HeadImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.HeadImgSourceProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(ChatMultyPicArticleItem.TitleProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.TitleProperty, value);
			}
		}
		public string NickName
		{
			get
			{
				return (string)base.GetValue(ChatMultyPicArticleItem.NickNameProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.NickNameProperty, value);
			}
		}
		public string UserInfo
		{
			get
			{
				return (string)base.GetValue(ChatMultyPicArticleItem.UserInfoProperty);
			}
			set
			{
				base.SetValue(ChatMultyPicArticleItem.UserInfoProperty, value);
			}
		}
		public ChatMultyPicArticleItem()
		{
			this.InitializeComponent();
			base.SetValue(MMTiltEffect.IsTiltEnabledProperty, false);
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~ChatMultyPicArticleItem()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ChatMultyPicArticleItem.ItemObjectProperty)
			{
				(dependencyObject_0 as ChatMultyPicArticleItem).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.mMsgInfo = this.ItemObject;
				this.RefreshItem(this.mMsgInfo);
			}
		}
		private void btnRoot_Click(object sender, RoutedEventArgs e)
		{
			if (this.getReaderItemCnt() == 1)
			{
				this.gotoItem(this.getItemAtIndex(0));
			}
		}
		private void btnTopItem_Click(object sender, RoutedEventArgs e)
		{
			this.gotoItem(this.getItemAtIndex(0));
		}
		private MMReaderItem getItemAtIndex(int nIndex)
		{
			if (this.mMsgInfo == null || this.mMsgInfo.mmreader == null || this.mMsgInfo.mmreader.items == null)
			{
				return null;
			}
			if (nIndex >= 0 && nIndex < this.mMsgInfo.mmreader.items.get_Count())
			{
				return this.mMsgInfo.mmreader.items.get_Item(nIndex);
			}
			return null;
		}
		private void ctrl_ItemClick(object sender, object object_0)
		{
			if (object_0 != null)
			{
				this.gotoItem(object_0 as MMReaderItem);
			}
		}
		private void gotoItem(MMReaderItem item)
		{
			if (item == null)
			{
				return;
			}
			if (this.mMsgInfo != null && this.mMsgInfo.itemType == 1)
			{
				TXNews tXNews = new TXNews();
				tXNews.strTitle = item.title;
				tXNews.strUrl = item.string_0;
				tXNews.strLongurl = item.longurl;
				tXNews.strShorturl = item.shorturl;
				tXNews.strTweetid = item.tweetid;
				tXNews.strCoverImgUrl = item.cover;
				tXNews.nPubTime = item.pub_time;
				tXNews.strDescipt = item.digest;
				if (this.mMsgInfo.mmreader != null)
				{
					tXNews.type = string.Concat(this.mMsgInfo.mmreader.type);
				}
				PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
				BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
				basePage.GoToPage("/Source/Plugin/TXNews/UI/NewsDetailInfoView.xaml", new object[]
				{
					tXNews
				}, false);
				return;
			}
			ToolFunction.GotoWebLinkInWebView(new WebViewPageConfig
			{
				strUrl = item.string_0,
				strTitle = item.title,
				mObj = item,
				strDigest = item.digest
			}, GetA8KeyScene.MMGETA8KEY_SCENE_UNKNOW);
		}
		private void ContentMenuItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.deleteTalkMsg(this.chatMsg_0);
		}
		private void DeleteMsgMenu_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemObject != null)
			{
				ChatMsg msg = StorageMgr.chatMsg.getMsg(this.ItemObject.nChatMsgLocalID);
				StorageMgr.chatMsg.delMsg(msg);
			}
		}
		private int getReaderItemCnt()
		{
			if (this.mMsgInfo != null && this.mMsgInfo.mmreader != null)
			{
				return this.mMsgInfo.mmreader.count;
			}
			return 0;
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ContentMenuItem = (base.GetTemplateChild("DeleteMsgMenu") as MenuItem);
			this.panelItems = (base.GetTemplateChild("panelItems") as Panel);
			this.txtTitle = (base.GetTemplateChild("txtTitle") as TextBlock);
			this.txtDigest = (base.GetTemplateChild("txtDigest") as TextBlock);
			this.txtTitleMulty = (base.GetTemplateChild("txtTitleMulty") as TextBlock);
			this.txtTime = (base.GetTemplateChild("txtTime") as TextBlock);
			this.TimeTxt2 = (base.GetTemplateChild("TimeTxt2") as TextBlock);
			this.imgCover = (base.GetTemplateChild("imgCover") as MMHttpImg);
			this.imgCover.imgContent.set_Stretch(3);
			this.btnTopItem = (base.GetTemplateChild("btnTopItem") as Button);
			this.btnTopItem.add_Click(new RoutedEventHandler(this.btnTopItem_Click));
			this.btnRoot = (base.GetTemplateChild("btnRoot") as Button);
			this.btnRoot.add_Click(new RoutedEventHandler(this.btnRoot_Click));
			this.ChatContentCtrl = (base.GetTemplateChild("ChatContentCtrl") as BaseChatContent);
			this.contextMenu = (base.GetTemplateChild("contextMenu") as ContextMenu);
			this.DeleteMsgMenu = (base.GetTemplateChild("DeleteMsgMenu") as MenuItem);
			this.DeleteMsgMenu.Click += new RoutedEventHandler(this.DeleteMsgMenu_Click);
			this.ChatContentCtrl.SetValue(MMTiltEffect.IsTiltEnabledProperty, false);
			this.mBIsTemplateApplyted = true;
			this.RefreshItem(this.mMsgInfo);
		}
		private void RefreshItem(AppMsgInfo msgInfo)
		{
			if (!this.mBIsTemplateApplyted)
			{
				return;
			}
			if (msgInfo != null && msgInfo.mmreader != null && msgInfo.mmreader.items != null && msgInfo.mmreader.items.get_Count() != 0)
			{
				List<MMReaderItem> items = msgInfo.mmreader.items;
				MMReaderItem mMReaderItem = items.get_Item(0);
				bool flag = this.getReaderItemCnt() > 1;
				if (msgInfo.itemType == 1)
				{
					this.contextMenu.set_IsEnabled(true);
					if (this.ItemObject != null)
					{
						ChatMsg msg = StorageMgr.chatMsg.getMsg(this.ItemObject.nChatMsgLocalID);
						if (msg != null)
						{
							this.TimeTxt2.set_Text(Util.formatTimeInChatMsg(msg.nCreateTime));
							this.TimeTxt2.set_Visibility(0);
						}
					}
				}
				else
				{
					this.contextMenu.set_IsEnabled(false);
					this.TimeTxt2.set_Visibility(1);
				}
				if (flag)
				{
					base.SetValue(TiltEffect.IsTiltEnabledProperty, false);
					this.txtTitle.set_Visibility(1);
					this.txtDigest.set_Visibility(1);
					this.txtTime.set_Visibility(1);
					this.txtTitleMulty.set_Visibility(0);
					this.txtTitleMulty.set_Text(mMReaderItem.title);
				}
				else
				{
					base.SetValue(TiltEffect.IsTiltEnabledProperty, true);
					this.txtTitleMulty.set_Visibility(1);
					this.txtTitle.set_Visibility(0);
					this.txtDigest.set_Visibility(0);
					this.txtTime.set_Visibility(0);
					this.txtTime.set_Text(Util.formatTimeInDays(mMReaderItem.pub_time));
					this.txtTitle.set_Text(mMReaderItem.title);
					this.txtDigest.set_Text(mMReaderItem.digest);
				}
				this.imgCover.imgContent.set_Source(null);
				if (string.IsNullOrWhiteSpace(mMReaderItem.cover))
				{
					this.imgCover.set_Visibility(1);
				}
				else
				{
					this.imgCover.SetUrl(mMReaderItem.cover);
					this.imgCover.set_Visibility(0);
				}
				this.setItemCnt(items.get_Count() - 1);
				for (int i = 1; i < items.get_Count(); i++)
				{
					mMReaderItem = items.get_Item(i);
					MMReaderItemCtrl mMReaderItemCtrl = this.panelItems.get_Children().get_Item(i - 1) as MMReaderItemCtrl;
					if (mMReaderItemCtrl != null)
					{
						mMReaderItemCtrl.ItemObject = mMReaderItem;
					}
				}
				return;
			}
			this.clearUI();
		}
		public void clearUI()
		{
			if (!this.mBIsTemplateApplyted)
			{
				return;
			}
			this.contextMenu.set_IsEnabled(false);
			this.TimeTxt2.set_Visibility(1);
			this.txtTitle.set_Visibility(1);
			this.txtDigest.set_Visibility(1);
			this.txtTime.set_Visibility(1);
			this.txtTitleMulty.set_Visibility(1);
			this.imgCover.imgContent.set_Source(null);
			this.setItemCnt(0);
		}
		public void setItemCnt(int nCnt)
		{
			if (nCnt < 0)
			{
				nCnt = 0;
			}
			if (this.panelItems.get_Children().get_Count() > nCnt)
			{
				while (this.panelItems.get_Children().get_Count() != nCnt)
				{
					MMReaderItemCtrl mMReaderItemCtrl = Enumerable.First<UIElement>(this.panelItems.get_Children()) as MMReaderItemCtrl;
					if (mMReaderItemCtrl != null)
					{
						mMReaderItemCtrl.ItemClick -= new MMReaderItemCtrl.ListItemClick(this.ctrl_ItemClick);
					}
					this.panelItems.get_Children().RemoveAt(0);
				}
				return;
			}
			if (this.panelItems.get_Children().get_Count() < nCnt)
			{
				while (this.panelItems.get_Children().get_Count() != nCnt)
				{
					MMReaderItemCtrl mMReaderItemCtrl2 = new MMReaderItemCtrl();
					mMReaderItemCtrl2.set_Margin(ChatMultyPicArticleItem.sMarginItems);
					mMReaderItemCtrl2.ItemClick += new MMReaderItemCtrl.ListItemClick(this.ctrl_ItemClick);
					this.panelItems.get_Children().Add(mMReaderItemCtrl2);
				}
			}
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			if (this.chatMsg_0 != null && this.chatMsg_0.IsAppMsg())
			{
				this.mMsgInfo = AppMsgMgr.parseAppXml(this.chatMsg_0.strMsg);
				if (this.mMsgInfo != null)
				{
					if (this.mMsgInfo.showtype == 1)
					{
						this.RefreshItem(this.mMsgInfo);
						return;
					}
				}
				return;
			}
		}
		private static void OnFlagImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnHeadImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnTitleChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnNickNameChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnUserInfoChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/MultyPicArticleMsg/ChatMultyPicArticleItem.xaml", 2));
		}
	}
}
